import { Modal } from 'antd'
import React, { useRef } from 'react'

import CustomForm from '../customForm'

const GatewayAddModal = (props: Record<string, any>) => {
  const { visible, cancel, title, callback, configs } = props
  const formRef = useRef<any>()

  const onCancel = () => {
    cancel && cancel()
  }

  const submit = async () => {
    const {
      current: { validateFields }
    } = formRef

    try {
      if (validateFields) {
        const values = await validateFields()
        callback && callback(values)
      }
    } catch (err) {
      console.log(err, 'error')
    }
  }

  return (
    <Modal
      title={title}
      visible={visible}
      onCancel={onCancel}
      onOk={submit}
      maskClosable={false}
      centered
    >
      <CustomForm configs={configs} ref={formRef}></CustomForm>
    </Modal>
  )
}

export default GatewayAddModal
